package com.sixth.shiro;

import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @ClassName AdminShiroSessionManager
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/9 9:47
 **/
@Component
public class AdminShiroSessionManager extends DefaultWebSessionManager {

    private static final String HEADER = "X-CskaoyanMarket-Admin-Token";

    private static final String WX_HEADER = "X-CskaoyanMarket-Token";


    // 会话一致性的保证
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;

        String sessionId = req.getHeader(HEADER);

        if (!StringUtil.isEmpty(sessionId)) {
            return sessionId;
        }

        String wxSessionId = req.getHeader(WX_HEADER);

        if (!StringUtil.isEmpty(wxSessionId)) {
            return wxSessionId;
        }

        return super.getSessionId(request, response);
    }
}
